package com.yinhe.music.yhmusic.network;


import com.yinhe.music.yhmusic.BuildConfig;
import com.yinhe.music.yhmusic.cache.CacheManager;
import com.yinhe.music.yhmusic.http.DefaultReqParam;
import com.yinhe.music.yhmusic.network.request.WebService;
import com.yinhe.music.yhmusic.utils.PackageUtil;
import com.yinhe.music.yhmusic.utils.Preferences;
import com.zhy.http.okhttp.https.HttpsUtils;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 *
 * API初始化类
 */

public class NetworkManager {

    private static NetworkManager mInstance;
    private static Retrofit retrofit;
    private static volatile WebService webService = null;
    private static final String HOST = BuildConfig.ENVIRONMENT;

    public static synchronized NetworkManager getInstance() {
        if (mInstance == null) {
            mInstance = new NetworkManager();
        }
        return mInstance;
    }

    /**
     * 初始化必要对象和参数
     */
    public void init() {

        HttpLoggingInterceptor logInterceptor = new HttpLoggingInterceptor();
        if(BuildConfig.DEBUG){
            //显示日志
            logInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        }else {
            logInterceptor.setLevel(HttpLoggingInterceptor.Level.NONE);
        }

        HttpsUtils.SSLParams sslParams = HttpsUtils.getSslSocketFactory(null, null, null);
        // 初始化okhttp
        OkHttpClient client = new OkHttpClient.Builder()
                .sslSocketFactory(sslParams.sSLSocketFactory, sslParams.trustManager)
                .addInterceptor(chain -> {
                    Request request = chain.request()
                            .newBuilder()
                            .addHeader(DefaultReqParam.SN, Preferences.getLoginSn())
                            .addHeader(DefaultReqParam.DEVID, PackageUtil.uuid.toString())
                            .addHeader(DefaultReqParam.APPVERSION, PackageUtil.strSoftWareVer)
                            .addHeader(DefaultReqParam.OS, "android")
                            .addHeader(DefaultReqParam.NATIONAL_TYPE, CacheManager.getInstance().getTypeLangId() + "")
                            .build();
                    return chain.proceed(request);
                })
                .addInterceptor(logInterceptor)
                .build();




        // 初始化Retrofit
        retrofit = new Retrofit.Builder()
                .client(client)
                .baseUrl(HOST)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    public static WebService getWebService() {
        if (webService == null) {
            synchronized (WebService.class) {
                webService = retrofit.create(WebService.class);
            }
        }
        return webService;
    }


}
